﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using DTO;
using Moved;

namespace BUS
{
    public class BoPhanBUS
    {
        private static String _connString = "";
        
        static BoPhanBUS()
        {
            _connString = ConfigurationManager.ConnectionStrings["WebChamCongConnectionString"].ConnectionString.ToString(); 
        }

        public static DTO.BoPhanDTO GetInforByDepartmentName(String Departmentname)
        {
            String NameDepartmentLocal = Departmentname;
            DTO.BoPhanDTO kq = null;
            using (Moved.Moved_DB dc = new Moved_DB(_connString))
            {
                var departments = (from depart in dc.AL_Department
                                   where string.Compare(depart.DepartmentName, NameDepartmentLocal) == 0
                                   select new
                                   {
                                       id = depart.DepartmentID,
                                       name = depart.DepartmentName,
                                       description = depart.Description

                                   }
                                     ).Distinct();
                foreach (var department in departments)
                {
                    kq = new BoPhanDTO();
                    kq.DepartmentName = department.name;
                    kq.DepartmentID = department.id;
                    kq.Description = department.description;

                }
                return kq;
            }
        }
        public static List<DTO.BoPhanDTO> GetBoPhans()
        {
            List<DTO.BoPhanDTO> localBoPhans = new List<BoPhanDTO>();
            
            using (Moved.Moved_DB dc = new Moved_DB(_connString))
            {
                // Get all department in database
                var objBoPhans = (from departs in dc.AL_Department                                            
                                           select new
                                            {
                                                id = departs.DepartmentID,
                                                code = departs.DepartmentCode,
                                                name = departs.DepartmentName,
                                                description = departs.Description
                                            }
                                          ).Distinct().OrderBy(item=>item.name);              

                // Each item in above list)
                foreach (var item in objBoPhans)
                {
                    DTO.BoPhanDTO itemBoPhan = new BoPhanDTO();
                    itemBoPhan.DepartmentID = item.id;
                    itemBoPhan.DepartmentCode = item.code;
                    itemBoPhan.DepartmentName = item.name;
                    itemBoPhan.Description = item.description;
                    localBoPhans.Add(itemBoPhan);
                }

            }

            return localBoPhans;
        }

        public static DTO.BoPhanDTO GetBoPhanById(int departID)
        {
            int localDepart = departID;

            DTO.BoPhanDTO localDepartment = null;

            using (Moved.Moved_DB dc = new Moved_DB(_connString))
            {
                // Get all department in database
                var objBoPhan = (from departs in dc.AL_Department
                                            where departs.DepartmentID == localDepart
                                            select new
                                            {
                                                id = departs.DepartmentID,
                                                code = departs.DepartmentCode,
                                                name = departs.DepartmentName,
                                                description = departs.Description
                                            }
                                          ).Distinct().OrderBy(item => item.name);
                foreach (var item in objBoPhan)
                {
                    localDepartment = new BoPhanDTO();
                    localDepartment.DepartmentID = item.id;
                    localDepartment.DepartmentCode = item.code;
                    localDepartment.DepartmentName = item.name;
                    localDepartment.Description = item.description;
                }

            }

            return localDepartment;
        }
    }
}
